﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using QLInternet.ViewModel;
using System.Collections.ObjectModel;
using QLInternet.Source;
namespace QLInternet.Model
{
    class util
    {
        public String GetSelectedCells(object sender, RoutedEventArgs e, DataGrid grid)
        {
            string selectedData = "";
            foreach (var dataGridCellInfo in grid.SelectedCells)
            {
                PropertyInfo pi = dataGridCellInfo.Item.GetType().GetProperty(dataGridCellInfo.Column.Header.ToString());
                var value = pi.GetValue(dataGridCellInfo.Item, null);
                selectedData += dataGridCellInfo.Column.Header + ": " + value.ToString() + "\n";
            }
            return selectedData;
        }

        public static int GetIndexByValueInSettingTypeComboBox(ComboBox cbb, string val)
        {
            for (int i = 0; i < cbb.Items.Count; i++)
            {
                if (((SettingType)cbb.Items[i]).TenKieu.Equals(val))
                    return i;
            }
            return 0;
        }

        public static int GetIndexByValueInModemComboBox(ComboBox cbb, string val)
        {
            for (int i = 0; i < cbb.Items.Count; i++)
            {
                if (((Modem)cbb.Items[i]).TenModem.Equals(val))
                    return i;
            }
            return 0;
        }

        public static string[][] convertDatagridTo2DString(DataGrid d)
        {
            return null;
        }





        public class ExportToExcel<T, U>
            where T : class
            where U : List<T>
        {
            public object[,] dataToPrint;
            public int NumberOfRows = 0;
            public object[] header;
            // Excel object references.
            private Excel.Application _excelApp = null;
            private Excel.Workbooks _books = null;
            private Excel._Workbook _book = null;
            private Excel.Sheets _sheets = null;
            private Excel._Worksheet _sheet = null;
            private Excel.Range _range = null;
            private Excel.Font _font = null;
            // Optional argument variable
            private object _optionalValue = Missing.Value;

            /// <summary>
            /// Generate report and sub functions
            /// </summary>
            public void GenerateReport()
            {
                try
                {
                    if (dataToPrint != null)
                    {
                        if (dataToPrint.Length != 0)
                        {
                            Mouse.SetCursor(Cursors.Wait);
                            CreateExcelRef();
                            FillSheet();
                            OpenReport();
                            Mouse.SetCursor(Cursors.Arrow);
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error while generating Excel report" + e.Message);
                }
                finally
                {
                    ReleaseObject(_sheet);
                    ReleaseObject(_sheets);
                    ReleaseObject(_book);
                    ReleaseObject(_books);
                    ReleaseObject(_excelApp);
                }
            }
            /// <summary>
            /// Make Microsoft Excel application visible
            /// </summary>
            private void OpenReport()
            {
                _excelApp.Visible = true;
            }
            /// <summary>
            /// Populate the Excel sheet
            /// </summary>
            private void FillSheet()
            {
                CreateHeader();
                WriteData();
            }
            /// <summary>
            /// Write data into the Excel sheet
            /// </summary>
            /// <param name="header"></param>
            private void WriteData()
            {
                //object[,] objData = new object[dataToPrint.Count, header.Length];

                
                AddExcelRows("A2", NumberOfRows, header.Length, dataToPrint);
                AutoFitColumns("A1", NumberOfRows + 1, header.Length);
            }
            /// <summary>
            /// Method to make columns auto fit according to data
            /// </summary>
            /// <param name="startRange"></param>
            /// <param name="rowCount"></param>
            /// <param name="colCount"></param>
            private void AutoFitColumns(string startRange, int rowCount, int colCount)
            {
                _range = _sheet.get_Range(startRange, _optionalValue);
                _range = _range.get_Resize(rowCount, colCount);
                _range.Columns.AutoFit();
                
            }
            /// <summary>
            /// Create header from the properties
            /// </summary>
            /// <returns></returns>
            private void CreateHeader()
            {

                
                AddExcelRows("A1", 1, header.Length, header);
                SetHeaderStyle();

                
            }
            /// <summary>
            /// Set Header style as bold
            /// </summary>
            private void SetHeaderStyle()
            {
                _font = _range.Font;
                _font.Bold = true;
            }
            /// <summary>
            /// Method to add an excel rows
            /// </summary>
            /// <param name="startRange"></param>
            /// <param name="rowCount"></param>
            /// <param name="colCount"></param>
            /// <param name="values"></param>
            private void AddExcelRows
            (string startRange, int rowCount, int colCount, object values)
            {
                _range = _sheet.get_Range(startRange, _optionalValue);
                _range = _range.get_Resize(rowCount, colCount);
                _range.set_Value(_optionalValue, values);
            }
            /// <summary>
            /// Create Excel application parameters instances
            /// </summary>
            private void CreateExcelRef()
            {
                _excelApp = new Excel.Application();
                _books = (Excel.Workbooks)_excelApp.Workbooks;
                _book = (Excel._Workbook)(_books.Add(_optionalValue));
                _sheets = (Excel.Sheets)_book.Worksheets;
                _sheet = (Excel._Worksheet)(_sheets.get_Item(1));
            }
            /// <summary>
            /// Release unused COM objects
            /// </summary>
            /// <param name="obj"></param>
            private void ReleaseObject(object obj)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                catch (Exception ex)
                {
                    obj = null;
                    MessageBox.Show(ex.Message.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }




    }
}
